import { createProject, getProjectList, updateProject, checkedCode, getLabels,refreshRedis } from '../services/project';

export default {
  namespace: 'project',
  state: {
    data: {},
    list: [],
    loading: false,
    labels: [],
  },

  effects: {
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(createProject, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *refreshRedis({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(refreshRedis, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },

    *update({ payload, callback }, { call, put }) {
      yield put({
        type: 'updateLoading',
        payload: true,
      });
      const response = yield call(updateProject, payload);
      yield put({
        type: 'saveUpdate',
        payload: response,
      });
      yield put({
        type: 'updateLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getProjectList, payload);
      yield put({
        type: 'appendList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *checkedCode({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(checkedCode, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *getLabels(_, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getLabels);
      yield put({
        type: 'saveLabels',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveUpdate(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveLabels(state, action) {
      return {
        ...state,
        labels: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    updateLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
